﻿using JSLCloud.App.Mes.Models;
using JSLCloud.App.Models.Xmkhgltx;
using JSLCloud.Attributes;
using JSLCloud.DBModel;
using JSLCloud.Framework;
using JSLCloud.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace JSLCloud.App.Mes.Service
{
    /// <summary>
    /// 后台线程服务
    /// </summary>
    [UseDI(ServiceLifetime.Scoped, typeof(IBackThreadService))]
    public class BackThreadService : IBackThreadService
    {
        private readonly IDbContext _db;//数据库操作实例对象
        private readonly AppConfig _appConfig;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="option"></param>
        public BackThreadService(IDbContext dbContext, IOptions<AppConfig> option)
        {
            _db = dbContext;
            _appConfig = option.Value;
        }

        /// <summary>
        /// 启动线程
        /// </summary>
        public void Start()
        {
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    DoCheckAuditMessage();

                    DoCheckAuditMessage3();

                    DoSetProcessNode();

                    DoCheckAuditMessage2();

                    DoCheckReceiveMessage();

                    DoCheckExpensesMessage();

                    DoCheckProcessMessage();

                    //2小时检查一次
                    Thread.Sleep(60 * 60 * 2 * 1000);
                }
            });
           
        }

        /// <summary>
        /// 根据过期待审核消息提醒
        /// </summary>
        private void DoCheckAuditMessage()
        {
            var currMysql = _db.Instance;
            try
            {
                currMysql.BeginTran();
                var curr = Convert.ToDateTime($"{DateTime.Now.ToString("yyyy-MM-dd")}");

                var queryData = currMysql.Queryable<XmkhgltxDbModel, XmjckhxxDbModel, XmjcgltxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                            JoinType.Left , t.Khid == t1.ID,
                            JoinType.Left , t.Txid == t2.ID,
                    })
                    .Where((t, t1, t2) => t.Zslb == "主证书" && t.Deleteflag == 0 && t.Qrzt == "Y")
                    .Select((t, t1, t2) => new XmkhgltxReportModel
                    {
                        ID = t.ID,
                        Khid = t.Khid,
                        Khmc = t1.Khmc,
                        Txid = t.Txid,
                        Txmc = t2.Txmc,
                        Mcshrq = (DateTime)t.Mcshrq,
                        Xcshrq = (DateTime)t.Xcshrq,
                        Zsrq = t.Zsrq,
                        Zsyxq = t.Zsyxq,
                        Zsh = t.Zsh,
                        Dshlx = t.Dqshlx
                    })
                    .ToList();
                
                
                foreach(var item in queryData)
                {
                    var end1 = item.Mcshrq.AddMonths(15);
                    var end2 = item.Mcshrq.AddMonths(27);
                    //撤销
                    if (curr > item.Zsyxq)
                    {                        
                        currMysql.Updateable<XmkhgltxDbModel>()
                           .SetColumns(p => p.Zszt == "撤销")
                           .Where(p => p.ID == item.ID)
                           .ExecuteCommand();
                    }

                    if (curr > end1)
                    {
                        //监一
                        var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                            .Where(p => p.Khid == item.Khid && p.Txid == item.Txid && p.Zsh == item.Zsh && p.Shxh == 1)
                            .First();
                        if (xmxx == null)
                        {
                            currMysql.Updateable<XmkhgltxDbModel>()
                           .SetColumns(p => p.Zszt == "撤销")
                           .Where(p => p.ID == item.ID)
                           .ExecuteCommand();
                        }
                    }
                    if (curr > end2)
                    {
                        //监二
                        var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                            .Where(p => p.Khid == item.Khid && p.Txid == item.Txid && p.Zsh == item.Zsh && p.Shxh == 2)
                            .First();
                        if (xmxx == null)
                        {
                            currMysql.Updateable<XmkhgltxDbModel>()
                           .SetColumns(p => p.Zszt == "撤销")
                           .Where(p => p.ID == item.ID)
                           .ExecuteCommand();
                        }
                    }
                }
                


                currMysql.CommitTran();
            }
            catch (Exception ex)
            {
                currMysql.RollbackTran();
            }

        }

        /// <summary>
        /// 设置进入监一、监二周期的状态信息
        /// </summary>
        private void DoCheckAuditMessage3()
        {
            var currMysql = _db.Instance;
            try
            {
                currMysql.BeginTran();
                var curr = Convert.ToDateTime($"{DateTime.Now.ToString("yyyy-MM-dd")}");

                var queryData = currMysql.Queryable<XmkhgltxDbModel, XmjckhxxDbModel, XmjcgltxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                            JoinType.Left , t.Khid == t1.ID,
                            JoinType.Left , t.Txid == t2.ID,
                    })
                    .Where((t, t1, t2) => t.Zslb == "主证书" && t.Deleteflag == 0 && t.Zszt != "撤销")
                    .Select((t, t1, t2) => new XmkhgltxReportModel
                    {
                        ID = t.ID,
                        Khid = t.Khid,
                        Khmc = t1.Khmc,
                        Txid = t.Txid,
                        Txmc = t2.Txmc,
                        Mcshrq = (DateTime)t.Mcshrq,
                        Xcshrq = (DateTime)t.Xcshrq,
                        Zsrq = t.Zsrq,
                        Zsyxq = t.Zsyxq,
                        Zsh = t.Zsh,
                        Dshlx = t.Dqshlx,
                        Fhxzmh = t.Fhxzmh,
                    })
                    .ToList();


                foreach (var item in queryData)
                {
                    if(item.Fhxzmh == "Y")
                    {
                        ///符合性证明函不提醒
                        continue;
                    }
                    var start1 = item.Mcshrq.AddMonths(9);
                    var end1 = item.Mcshrq.AddMonths(15);
                    var start2 = item.Mcshrq.AddMonths(21);
                    var end2 = item.Mcshrq.AddMonths(27);
                    if (curr >= start1 && curr <= end1)
                    {
                        //监一
                        var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                            .Where(p => p.Khid == item.Khid && p.Txid == item.Txid && p.Zsh == item.Zsh && p.Shxh == 1)
                            .First();
                        if (xmxx == null)
                        {
                            currMysql.Updateable<XmkhgltxDbModel>()
                               .SetColumns(p => p.Dshlx == "监一")
                               .Where(p => p.ID == item.ID)
                               .ExecuteCommand();
                        }
                        else
                        {
                            currMysql.Updateable<XmkhgltxDbModel>()
                               .SetColumns(p => p.Dshlx == "")
                               .Where(p => p.ID == item.ID)
                               .ExecuteCommand();
                        }
                    }
                    if (curr >= start2 && curr <= end2)
                    {
                        //监一
                        var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                            .Where(p => p.Khid == item.Khid && p.Txid == item.Txid && p.Zsh == item.Zsh && p.Shxh == 2)
                            .First();
                        if (xmxx == null)
                        {
                            currMysql.Updateable<XmkhgltxDbModel>()
                               .SetColumns(p => p.Dshlx == "监二")
                               .Where(p => p.ID == item.ID)
                               .ExecuteCommand();
                        }
                        else
                        {
                            currMysql.Updateable<XmkhgltxDbModel>()
                               .SetColumns(p => p.Dshlx == "")
                               .Where(p => p.ID == item.ID)
                               .ExecuteCommand();
                        }
                    }
                }



                currMysql.CommitTran();
            }
            catch (Exception ex)
            {
                currMysql.RollbackTran();
            }

        }

        /// <summary>
        /// 根据设置的提醒时间 生成待审核消息
        /// </summary>
        private void DoCheckAuditMessage2()
        {
            var currMysql = _db.Instance;
            try
            {
                currMysql.BeginTran();                            

                #region 所有证书信息
                var queryData = currMysql.Queryable<XmkhgltxDbModel, XmjckhxxDbModel, XmjcgltxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                            JoinType.Left , t.Khid == t1.ID,
                            JoinType.Left , t.Txid == t2.ID,
                    })
                    .Where((t, t1, t2) => t.Zslb == "主证书" && t.Deleteflag == 0)
                    .Select((t, t1, t2) => new XmkhgltxReportModel
                    {
                        ID = t.ID,
                        Khid = t.Khid,
                        Khmc = t1.Khmc,
                        Txid = t.Txid,
                        Txmc = t2.Txmc,
                        Mcshrq = (DateTime)t.Mcshrq,
                        Xcshrq = (DateTime)t.Xcshrq,
                        Zsrq = t.Zsrq,
                        Zsyxq = t.Zsyxq,
                        Zsh = t.Zsh,
                        Dshlx = t.Dqshlx
                    })
                    .ToList();
                #endregion

                #region 监一消息提醒
                var jyList = queryData.Where(p => p.Dshlx != "监一" && p.Dshlx != "监二").ToList();
                var userList = currMysql.Queryable<TpmusermessageDbModel, TpmmessagetypeDbModel>(
                    (t, t1) => new object[]
                    {
                            JoinType.Left , t.Messagetypeid == t1.ID
                    })
                    .Where((t, t1) => t1.Xxlx == "监一审核")
                    .ToList();
                var currDay = DateTime.Now.Date;
                foreach (var zs in jyList)
                {
                    foreach (var item in userList)
                    {
                        //过期前提醒
                        if (item.Txlx == 0)
                        {
                            var tq = zs.Xcshrq.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (tq <= currDay && currDay <= zs.Xcshrq)
                            {
                                var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                                    .Where(p => p.Khid == zs.Khid && p.Txid == zs.Txid && p.Zsh == zs.Zsh && p.Shxh == 1)
                                    .First();
                                if (xmxx == null)
                                {
                                    //过期前提醒
                                    var msg = new TpmmessageDbModel
                                    {
                                        ID = 0,
                                        Xxdqzt = "N",
                                        Xxfssj = DateTime.Now,
                                        Xxjsr = item.Userid,
                                        Xxjssj = null,
                                        Xxnr = $"{zs.Khmc}的{zs.Txmc}证书待监一审核，请关注!",
                                        Sjid = zs.ID,
                                        Xxlx = item.Messagetypeid,
                                        Title = "待监一审核"
                                    };
                                    try
                                    {
                                        currMysql.Insertable(msg).ExecuteCommand();
                                    }
                                    catch
                                    {
                                        //执行失败说明已有提醒消息
                                    }
                                }
                                
                            }

                        }
                        else
                        {
                            //过期前提醒，按天数提醒
                            var tq = zs.Xcshrq.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (tq <= DateTime.Now)
                            {
                                var ytxcs = currMysql.Queryable<TpmmessageDbModel>()
                                    .Where(p => p.Xxjsr == item.Userid && p.Xxlx == item.Messagetypeid
                                    && p.Sjid == zs.ID && p.Xxfssj >= tq)
                                    .Count();
                                if (item.Txts == null && ytxcs < item.Txts)
                                {
                                    //未设置提醒天数 或者小于提醒天数都进行提醒
                                    var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                                    .Where(p => p.Khid == zs.Khid && p.Txid == zs.Txid && p.Zsh == zs.Zsh && p.Shxh == 1)
                                    .First();
                                    if (xmxx == null)
                                    {
                                        //过期前提醒
                                        var msg = new TpmmessageDbModel
                                        {
                                            ID = 0,
                                            Xxdqzt = "N",
                                            Xxfssj = DateTime.Now,
                                            Xxjsr = item.Userid,
                                            Xxjssj = null,
                                            Xxnr = $"{zs.Khmc}的{zs.Txmc}证书待监一审核，请关注!",
                                            Sjid = zs.ID,
                                            Xxlx = item.Messagetypeid,
                                            Title = "待监一审核"
                                        };
                                        try
                                        {
                                            currMysql.Insertable(msg).ExecuteCommand();
                                        }
                                        catch
                                        {
                                            //执行失败说明已有提醒消息
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion

                #region 监二消息提醒
                var jeList = queryData.Where(p => p.Dshlx == "监一").ToList();
                var user2List = currMysql.Queryable<TpmusermessageDbModel, TpmmessagetypeDbModel>(
                   (t, t1) => new object[]
                   {
                            JoinType.Left , t.Messagetypeid == t1.ID
                   })
                   .Where((t, t1) => t1.Xxlx == "监二审核")
                   .ToList();
                foreach (var zs in jeList)
                {
                    foreach (var item in user2List)
                    {
                        //过期前提醒
                        if (item.Txlx == 0)
                        {
                            var tq = zs.Xcshrq.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (tq <= currDay && currDay <= zs.Xcshrq)
                            {
                                var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                                    .Where(p => p.Khid == zs.Khid && p.Txid == zs.Txid && p.Zsh == zs.Zsh && p.Shxh == 2)
                                    .First();
                                if (xmxx == null)
                                {
                                    //过期前提醒
                                    var msg = new TpmmessageDbModel
                                    {
                                        ID = 0,
                                        Xxdqzt = "N",
                                        Xxfssj = DateTime.Now,
                                        Xxjsr = item.Userid,
                                        Xxjssj = null,
                                        Xxnr = $"{zs.Khmc}的{zs.Txmc}证书待监二审核，请关注!",
                                        Sjid = zs.ID,
                                        Xxlx = item.Messagetypeid,
                                        Title = "待监二审核"
                                    };
                                    try
                                    {
                                        currMysql.Insertable(msg).ExecuteCommand();
                                    }
                                    catch
                                    {
                                        //执行失败说明已有提醒消息
                                    }
                                }
                            }

                        }
                        else
                        {
                            //过期前提醒，按天数提醒
                            var tq = zs.Xcshrq.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (tq <= currDay && currDay <= zs.Xcshrq)
                            {
                                var ytxcs = currMysql.Queryable<TpmmessageDbModel>()
                                    .Where(p => p.Xxjsr == item.Userid && p.Xxlx == item.Messagetypeid
                                    && p.Sjid == zs.ID && p.Xxfssj >= tq)
                                    .Count();
                                if (item.Txts == null && ytxcs < item.Txts)
                                {
                                    //未设置提醒天数 或者小于提醒天数都进行提醒
                                    var xmxx = currMysql.Queryable<XmglxmxxDbModel>()
                                    .Where(p => p.Khid == zs.Khid && p.Txid == zs.Txid && p.Zsh == zs.Zsh && p.Shxh == 2)
                                    .First();
                                    if (xmxx == null)
                                    {
                                        //过期前提醒
                                        var msg = new TpmmessageDbModel
                                        {
                                            ID = 0,
                                            Xxdqzt = "N",
                                            Xxfssj = DateTime.Now,
                                            Xxjsr = item.Userid,
                                            Xxjssj = null,
                                            Xxnr = $"{zs.Khmc}的{zs.Txmc}证书待监一审核，请关注!",
                                            Sjid = zs.ID,
                                            Xxlx = item.Messagetypeid,
                                            Title = "待监二审核"
                                        };
                                        try
                                        {
                                            currMysql.Insertable(msg).ExecuteCommand();
                                        }
                                        catch
                                        {
                                            //执行失败说明已有提醒消息
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion

                currMysql.CommitTran();
            }
            catch (Exception ex) 
            {
                currMysql.RollbackTran();
            }
        }

        /// <summary>
        /// 待收款
        /// </summary>
        private void DoCheckReceiveMessage()
        {           
            var currMysql = _db.Instance;
            try
            {
                currMysql.BeginTran();
                var curr = Convert.ToDateTime($"{DateTime.Now.ToString("yyyy-MM-dd")}");
                var dskList = currMysql.Queryable<XmglxmxxskDbModel,XmglxmxxDbModel,XmjckhxxDbModel>(
                    (t,t1,t2) => new object[]
                    {
                        JoinType.Left , t.Xmid == t1.ID,
                        JoinType.Left , t1.Khid == t2.ID
                    })
                    .Where((t,t1,t2) => t.Skzt == "N" && t.Deleteflag == 0)
                    .Select((t,t1,t2) => new
                    {
                        t.ID,
                        t.Skxm,
                        t.Ysrq,
                        t2.Khmc
                    })
                    .ToList();
                var userList = currMysql.Queryable<TpmusermessageDbModel, TpmmessagetypeDbModel>(
                    (t, t1) => new object[]
                    {
                            JoinType.Left , t.Messagetypeid == t1.ID
                    })
                    .Where((t, t1) => t1.Xxlx == "待收款")
                    .ToList();

                foreach (var dsk in dskList)
                {
                    foreach (var item in userList)
                    {
                        //过期前提醒
                        if (item.Txlx == 0)
                        {
                            var tq = dsk.Ysrq.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (curr >= tq && curr <= dsk.Ysrq)
                            {
                                //过期前提醒
                                var msg = new TpmmessageDbModel
                                {
                                    ID = 0,
                                    Xxdqzt = "N",
                                    Xxfssj = DateTime.Now,
                                    Xxjsr = item.Userid,
                                    Xxjssj = null,
                                    Xxnr = $"{dsk.Khmc}的款项\"{dsk.Skxm}\"应该收款了，请关注!",
                                    Sjid = dsk.ID,
                                    Xxlx = item.Messagetypeid,
                                    Title = "待收款"
                                };
                                try
                                {
                                    currMysql.Insertable(msg).ExecuteCommand();
                                }
                                catch
                                {
                                    //执行失败说明已有提醒消息
                                }
                            }

                        }
                        else
                        {
                            //过期前提醒，按天数提醒
                            var tq = dsk.Ysrq.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (curr >= tq && curr <= dsk.Ysrq)
                            {
                                var ytxcs = currMysql.Queryable<TpmmessageDbModel>()
                                    .Where(p => p.Xxjsr == item.Userid && p.Xxlx == item.Messagetypeid
                                    && p.Sjid == dsk.ID && p.Xxfssj >= tq)
                                    .Count();
                                if (item.Txts == null && ytxcs < item.Txts)
                                {
                                    //未设置提醒天数 或者小于提醒天数都进行提醒

                                    //过期前提醒
                                    var msg = new TpmmessageDbModel
                                    {
                                        ID = 0,
                                        Xxdqzt = "N",
                                        Xxfssj = DateTime.Now,
                                        Xxjsr = item.Userid,
                                        Xxjssj = null,
                                        Xxnr = $"{dsk.Khmc}的款项\"{dsk.Skxm}\"应该收款了，请关注!",
                                        Sjid = dsk.ID,
                                        Xxlx = item.Messagetypeid,
                                        Title = "待收款"
                                    };
                                    try
                                    {
                                        currMysql.Insertable(msg).ExecuteCommand();
                                    }
                                    catch
                                    {
                                        //执行失败说明已有提醒消息
                                    }
                                }
                            }
                        }
                    }
                }

                currMysql.CommitTran();
            }
            catch (Exception ex)
            {
                currMysql.RollbackTran();
            }

        }

        /// <summary>
        /// 设置流程节点提醒时间
        /// </summary>
        private void DoSetProcessNode()
        {
            var currMysql = _db.Instance;
            try
            {
                currMysql.BeginTran();

                //查找所有设置了流程节点提醒的项目信息
                var data = currMysql.Queryable<XmglxmxxlcDbModel>().Where(p => p.Xxtxlx != null && p.Txsj == null).ToList();
                var xxlx = currMysql.Queryable<TpmmessagetypeDbModel>().ToList();

                foreach(var item in data)
                {
                    var currXxlx = xxlx.Where(p => p.ID == item.Xxtxlx).First();
                    if (currXxlx == null)
                        continue;
                    var maxDate = (DateTime)currMysql.Queryable<XmglxmxxlcDbModel>()
                        .Where(p => p.Xmid == item.Xmid && p.Sfxcsh == "Y").Max(p => p.Jsrq);
                    var txsj = maxDate.AddDays(40);
                    if (currXxlx.Txzq != null)
                    {
                        txsj = maxDate.AddDays((int)currXxlx.Txzq);
                    }
                    currMysql.Updateable<XmglxmxxlcDbModel>()
                        .SetColumns(p => p.Txsj == txsj)
                        .Where(p => p.ID == item.ID)
                        .ExecuteCommand();
                }
                

                currMysql.CommitTran();
            }
            catch (Exception ex)
            {
                currMysql.RollbackTran();
            }
        }

        /// <summary>
        /// 流程节点消息提醒
        /// </summary>
        private void DoCheckProcessMessage()
        {
            var currMysql = _db.Instance;
            try
            {
                currMysql.BeginTran();
                var curr = Convert.ToDateTime($"{DateTime.Now.ToString("yyyy-MM-dd")}");
                var processList = currMysql.Queryable<XmglxmxxlcDbModel, XmglxmxxDbModel, XmjckhxxDbModel, XmjcgltxDbModel>(
                    (t, t1, t2, t3) => new object[]
                    {
                        JoinType.Left , t.Xmid == t1.ID,
                        JoinType.Left , t1.Khid == t2.ID,
                        JoinType.Left , t1.Txid == t3.ID
                    })
                    .Where((t, t1, t2, t3) => t.Txsj != null && t.Lczt == "N")
                    .Select((t, t1, t2, t3) => new
                    {
                        t.ID,
                        t1.Shlx,
                        t2.Khmc,
                        t3.Txmc,
                        t.Lcmc,
                        t.Txsj
                    })
                    .ToList();
                var userList = currMysql.Queryable<TpmusermessageDbModel, TpmmessagetypeDbModel>(
                    (t, t1) => new object[]
                    {
                            JoinType.Left , t.Messagetypeid == t1.ID
                    })
                    .Where((t, t1) => t1.Xxlx == "不符合项关闭")
                    .ToList();

                foreach (var process in processList)
                {
                    foreach (var item in userList)
                    {
                        //过期前提醒
                        if (item.Txlx == 0)
                        {
                            var txsj = (DateTime)process.Txsj;
                            var tq = txsj.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (curr >= tq && curr <= txsj)
                            {
                                //过期前提醒
                                var msg = new TpmmessageDbModel
                                {
                                    ID = 0,
                                    Xxdqzt = "N",
                                    Xxfssj = DateTime.Now,
                                    Xxjsr = item.Userid,
                                    Xxjssj = null,
                                    Xxnr = $"{process.Khmc}的{process.Txmc}流程节点'{process.Lcmc}'未完成，请关注!",
                                    Sjid = process.ID,
                                    Xxlx = item.Messagetypeid,
                                    Title = "不符合项关闭"
                                };
                                try
                                {
                                    currMysql.Insertable(msg).ExecuteCommand();
                                }
                                catch
                                {
                                    //执行失败说明已有提醒消息
                                }
                            }

                        }
                        else
                        {
                            //过期前提醒，按天数提醒
                            var txsj = (DateTime)process.Txsj;
                            var tq = txsj.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (curr >= tq && curr <= txsj)
                            {
                                var ytxcs = currMysql.Queryable<TpmmessageDbModel>()
                                    .Where(p => p.Xxjsr == item.Userid && p.Xxlx == item.Messagetypeid
                                    && p.Sjid == process.ID && p.Xxfssj >= tq)
                                    .Count();
                                if (item.Txts == null && ytxcs < item.Txts)
                                {
                                    //未设置提醒天数 或者小于提醒天数都进行提醒

                                    //过期前提醒
                                    var msg = new TpmmessageDbModel
                                    {
                                        ID = 0,
                                        Xxdqzt = "N",
                                        Xxfssj = DateTime.Now,
                                        Xxjsr = item.Userid,
                                        Xxjssj = null,
                                        Xxnr = $"{process.Khmc}的{process.Txmc}流程节点'{process.Lcmc}'未完成，请关注!",
                                        Sjid = process.ID,
                                        Xxlx = item.Messagetypeid,
                                        Title = "不符合项关闭"
                                    };
                                    try
                                    {
                                        currMysql.Insertable(msg).ExecuteCommand();
                                    }
                                    catch
                                    {
                                        //执行失败说明已有提醒消息
                                    }
                                }
                            }
                        }
                    }
                }

                currMysql.CommitTran();
            }
            catch (Exception ex)
            {
                currMysql.RollbackTran();
            }

        }

        /// <summary>
        /// 待付款
        /// </summary>
        private void DoCheckExpensesMessage()
        {
            var currMysql = _db.Instance;
            try
            {
                currMysql.BeginTran();
                var curr = Convert.ToDateTime($"{DateTime.Now.ToString("yyyy-MM-dd")}");
                var dskList = currMysql.Queryable<XmglxmxxkzDbModel, XmglxmxxDbModel, XmjckhxxDbModel>(
                    (t, t1, t2) => new object[]
                    {
                        JoinType.Left , t.Xmid == t1.ID,
                        JoinType.Left , t1.Khid == t2.ID
                    })
                    .Where((t, t1, t2) => t.Kzzt == "N" && t.Deleteflag == 0)
                    .Select((t, t1, t2) => new
                    {
                        t.ID,
                        t.Kzxm,
                        t.Kzsj,
                        t2.Khmc
                    })
                    .ToList();
                var userList = currMysql.Queryable<TpmusermessageDbModel, TpmmessagetypeDbModel>(
                    (t, t1) => new object[]
                    {
                            JoinType.Left , t.Messagetypeid == t1.ID
                    })
                    .Where((t, t1) => t1.Xxlx == "待付款")
                    .ToList();

                foreach (var dsk in dskList)
                {
                    foreach (var item in userList)
                    {
                        //过期前提醒
                        if (item.Txlx == 0)
                        {
                            var tq = dsk.Kzsj.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (curr >= tq && curr <= dsk.Kzsj)
                            {
                                //过期前提醒
                                var msg = new TpmmessageDbModel
                                {
                                    ID = 0,
                                    Xxdqzt = "N",
                                    Xxfssj = DateTime.Now,
                                    Xxjsr = item.Userid,
                                    Xxjssj = null,
                                    Xxnr = $"{dsk.Khmc}的款项\"{dsk.Kzxm}\"应该付款了，请关注!",
                                    Sjid = dsk.ID,
                                    Xxlx = item.Messagetypeid,
                                    Title = "待付款"
                                };
                                try
                                {
                                    currMysql.Insertable(msg).ExecuteCommand();
                                }
                                catch
                                {
                                    //执行失败说明已有提醒消息
                                }
                            }

                        }
                        else
                        {
                            //过期前提醒，按天数提醒
                            var tq = dsk.Kzsj.AddDays(item.Tqts * -1);
                            //在提前提醒周期内进行提醒
                            if (curr >= tq && curr <= dsk.Kzsj)
                            {
                                var ytxcs = currMysql.Queryable<TpmmessageDbModel>()
                                    .Where(p => p.Xxjsr == item.Userid && p.Xxlx == item.Messagetypeid
                                    && p.Sjid == dsk.ID && p.Xxfssj >= tq)
                                    .Count();
                                if (item.Txts == null && ytxcs < item.Txts)
                                {
                                    //未设置提醒天数 或者小于提醒天数都进行提醒

                                    //过期前提醒
                                    var msg = new TpmmessageDbModel
                                    {
                                        ID = 0,
                                        Xxdqzt = "N",
                                        Xxfssj = DateTime.Now,
                                        Xxjsr = item.Userid,
                                        Xxjssj = null,
                                        Xxnr = $"{dsk.Khmc}的款项\"{dsk.Kzxm}\"应该付款了，请关注!",
                                        Sjid = dsk.ID,
                                        Xxlx = item.Messagetypeid,
                                        Title = "待付款"
                                    };
                                    try
                                    {
                                        currMysql.Insertable(msg).ExecuteCommand();
                                    }
                                    catch
                                    {
                                        //执行失败说明已有提醒消息
                                    }
                                }
                            }
                        }
                    }
                }

                currMysql.CommitTran();
            }
            catch (Exception ex)
            {
                currMysql.RollbackTran();
            }

        }

    }
}
